/*---------------------------------------------------------------------------------

	by GugloPWN

---------------------------------------------------------------------------------*/

#include "block.h"



void block::readblock(char path[64])
{
	char mapfile[128] = "";
	char modelfile[128] = "";
	strcat(mapfile,path);
	strcat(mapfile,".vmap");
	strcat(modelfile,path);
	strcat(modelfile,".md2");
	
	FILE* file = fopen(mapfile,"rb");
	
	if(file == NULL) 
	{
		printf("\n%s block not found",mapfile);
	}
	else
	{
		printf("\n%s block found",mapfile);
		u8 temp;
		int size = sizeof(temp);

		int i,j;
		for(i=0;i<20;i++)
		{
			for(j=0;j<20;j++)
			{
				fread(&temp,size,1,file);
				base[i][j] = temp;
				fread(&temp,size,1,file);
				plat[i][j] = temp;
			}
		}
		rail rtemp;
		size = sizeof(rtemp);
		for(i=0;i<16;i++)
		{
			fread(&rtemp,size,1,file);
			rails[i] = rtemp;
		}
		tag ttemp;
		size = sizeof(ttemp);
		for(i=0;i<6;i++)
		{
			fread(&ttemp,size,1,file);
			tags[i] = ttemp;
		}
		deco dtemp;
		size = sizeof(dtemp);
		for(i=0;i<16;i++)
		{
			fread(&dtemp,size,1,file);
			decos[i] = dtemp;
		}
		pole ptemp;
		size = sizeof(ptemp);
		for(i=0;i<16;i++)
		{
			fread(&ptemp,size,1,file);
			poles[i] = ptemp;
		}
		item itemp;
		size = sizeof(itemp);
		for(i=0;i<16;i++)
		{
			fread(&itemp,size,1,file);
			items[i] = itemp;
		}
		
	
		fclose(file);
	}
	
	/*
	printf("Loading Block Texture\n");
	glGenTextures(1, Texture);
	LoadModelTexture(textfile,Texture,TexturePal,&textsize) ;
	*/
	printf("Loading Block Model\n");
	LoadMD2Model(modelfile,256,1,Model,ModelEnable);
	printf("Done.\n");
	
	
	return;
}

void block::writeblock(char path[64])
{
	char mapfile[128] = "";
	strcat(mapfile,path);
	strcat(mapfile,".vmap");
	
	FILE* file = fopen(mapfile,"wb");
	
	u8 temp;
	int size = sizeof(temp);

	int i,j;
	for(i=0;i<20;i++)
	{
		for(j=0;j<20;j++)
		{
			temp = base[i][j];
			fwrite(&temp,size,1,file);
			temp = plat[i][j];
			fwrite(&temp,size,1,file);
		}
	}
	rail rtemp;
	size = sizeof(rtemp);
	for(i=0;i<16;i++)
	{
		rtemp = rails[i];
		fwrite(&rtemp,size,1,file);
	}
	tag ttemp;
	size = sizeof(ttemp);
	for(i=0;i<6;i++)
	{
		ttemp = tags[i];
		fwrite(&ttemp,size,1,file);
		tags[i] = ttemp;
	}
	deco dtemp;
	size = sizeof(dtemp);
	for(i=0;i<16;i++)
	{
		dtemp = decos[i];
		fwrite(&dtemp,size,1,file);
	}
	pole ptemp;
	size = sizeof(ptemp);
	for(i=0;i<16;i++)
	{
		ptemp = poles[i];
		fwrite(&ptemp,size,1,file);
	}
	item itemp;
	size = sizeof(itemp);
	for(i=0;i<16;i++)
	{
		itemp = items[i];
		fwrite(&itemp,size,1,file);
	}
	
	
	
	fclose(file);
	return;
}

void block::getdefaultblock()
{
	int i,j;
	for(i=0;i<20;i++)
	{
		for(j=0;j<20;j++)
		{
			base[i][j] = 0;
			plat[i][j] = 0;
		}
	}
	
	//Rails
	for(i=0;i<16;i++)
	{
		rails[i].a.x = 21;
		rails[i].a.y = 21;
		rails[i].a.z = 0;
		rails[i].b.x = 21;
		rails[i].b.y = 21;
		rails[i].b.z = 0;
	}
	
	//Tags
	for(i=0;i<16;i++)
	{
		tags[i].x = 21;
		tags[i].y = 21;
		tags[i].z = 0;
		tags[i].rot = 0;
		tags[i].size = 0;
	}
	
	//Deco
	for(i=0;i<16;i++)
	{
		decos[i].x = 21;
		decos[i].y = 21;
		decos[i].z = 0;
		decos[i].rot = 0;
		decos[i].type = 0;
	}
	
	//Poles
	for(i=0;i<16;i++)
	{
		poles[i].x = 0;
		poles[i].y = 0;
		poles[i].z = 0;
	}
	
	//Items
	for(i=0;i<16;i++)
	{
		items[i].x = 0;
		items[i].y = 0;
		items[i].z = 0;
		items[i].type = 0;
	}
}

void block::getnullblock()
{
	int i,j;
	for(i=0;i<20;i++)
	{
		for(j=0;j<20;j++)
		{
			base[i][j] = 100;
			plat[i][j] = 100;
		}
	}
	
	//Rails
	for(i=0;i<16;i++)
	{
		rails[i].a.x = 21;
		rails[i].a.y = 21;
		rails[i].a.z = 0;
		rails[i].b.x = 21;
		rails[i].b.y = 21;
		rails[i].b.z = 0;
	}
	
	//Tags
	
	//Deco
	for(i=0;i<16;i++)
	{
		decos[i].x = 21;
		decos[i].y = 21;
		decos[i].z = 0;
		decos[i].rot = 0;
		decos[i].type = 0;
	}
	
	//Poles
	for(i=0;i<16;i++)
	{
		poles[i].x = 0;
		poles[i].y = 0;
		poles[i].z = 0;
	}
	
	
	//Items
}


void block::loadblockmodel()
{
	/*
	printf("Loading Block Texture\n");
	glGenTextures(10, &Texture);
	LoadModelTexture("warehouse.bmp",Texture,TexturePal,&textsize) ;
	printf("Loading Block Model\n");
	LoadMD2Model("warehouse.md2",textsize,1,Model,ModelEnable);
	printf("Done.\n");
	*/
}

void block::drawblock()
{
	if(ModelEnable == true)
	{

		glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE);
		glPushMatrix();
	
	glRotatef(270,0.0f,0.0f,1.0f);
	glScalef(12.8,12.8,12.8);
	
		//glBindTexture (GL_TEXTURE_2D, Texture[0]);
		//glColorTable(GL_RGB256, TexturePal[0]);		
		RenderMD2Model(0,Model,ModelEnable);
	
		glPopMatrix(1);
	}
}

void block::drawblockcolmap()
{
	//if(ModelEnable == false)
	//{
		glPolyFmt(POLY_ALPHA(0) | POLY_CULL_NONE);
		
		//glLoadIdentity(); 
		//glRotatef(-30,1.0f,0,0);
		//glTranslatef(-1.40,0,-1.6f);
		//glPushMatrix();
		//glTranslatef(-0.05,-0.05,0.0f);
	
	
	
	
		int i,j;
		glBegin(GL_QUADS);
		for(i=0;i<20;i++)
		{
			for(j=0;j<20;j++)
			{
				//Draw the Base
				glVertex3f( ((float)i)/10, ((float)j)/10, ((float)base[i][j])/40);
				glVertex3f( ((float)(i+1))/10, ((float)j)/10, ((float)base[i][j])/40);
				glVertex3f( ((float)(i+1))/10, ((float)(j+1))/10, ((float)base[i][j])/40);
				glVertex3f( ((float)(i))/10, ((float)(j+1))/10, ((float)base[i][j])/40);
			
				//Draw the platform
				if(plat[i][j] != 0)
				{
					glColor3f(0.0f,0.0f,0.0f);
					glVertex3f( ((float)i)/10, ((float)j)/10, ((float)plat[i][j])/40);
					glVertex3f( ((float)(i+1))/10, ((float)j)/10, ((float)plat[i][j])/40);
					glVertex3f( ((float)(i+1))/10, ((float)(j+1))/10, ((float)plat[i][j])/40);
					glVertex3f( ((float)(i))/10, ((float)(j+1))/10, ((float)plat[i][j])/40);
					glColor3f(1.0f,1.0f,1.0f);
				}
			}
		}
		
		//Rails
		glColor3f(0.0f,1.0f,0.0f);
		for(i=0;i<16;i++)
		{
			if(rails[i].a.x != 21)
			{
				glVertex3f( ((float)rails[i].a.x)/10,((float)rails[i].a.y)/10,((float)rails[i].a.z)/40);
				glVertex3f( ((float)rails[i].a.x)/10,((float)rails[i].a.y)/10,((float)rails[i].a.z-0.05)/40);
				glVertex3f( ((float)rails[i].b.x)/10,((float)rails[i].b.y)/10,((float)rails[i].b.z-0.05)/40);
				glVertex3f( ((float)rails[i].b.x)/10,((float)rails[i].b.y)/10,((float)rails[i].b.z)/40);
			}
		}
		
		//Tags
		glColor3f(1.0f,0.0f,0.0f);
		for(i=0;i<6;i++)
		{
			if(tags[i].size != 0)
			{
				if(tags[i].rot == 0)
				{
					glVertex3f( ((float)tags[i].x+0.95)/10,((float)tags[i].y+0.5+0.5*tags[i].size)/10,((float)tags[i].z+4*tags[i].size)/40);
					glVertex3f( ((float)tags[i].x+0.95)/10,((float)tags[i].y+0.5+0.5*tags[i].size)/10,((float)tags[i].z)/40);
					glVertex3f( ((float)tags[i].x+0.95)/10,((float)tags[i].y+0.5-0.5*tags[i].size)/10,((float)tags[i].z)/40);
					glVertex3f( ((float)tags[i].x+0.95)/10,((float)tags[i].y+0.5-0.5*tags[i].size)/10,((float)tags[i].z+4*tags[i].size)/40);
				}
				else if(tags[i].rot == 2)
				{
					glVertex3f( ((float)tags[i].x+0.05)/10,((float)tags[i].y+0.5+0.5*tags[i].size)/10,((float)tags[i].z+4*tags[i].size)/40);
					glVertex3f( ((float)tags[i].x+0.05)/10,((float)tags[i].y+0.5+0.5*tags[i].size)/10,((float)tags[i].z)/40);
					glVertex3f( ((float)tags[i].x+0.05)/10,((float)tags[i].y+0.5-0.5*tags[i].size)/10,((float)tags[i].z)/40);
					glVertex3f( ((float)tags[i].x+0.05)/10,((float)tags[i].y+0.5-0.5*tags[i].size)/10,((float)tags[i].z+4*tags[i].size)/40);
				}
				if(tags[i].rot == 3)
				{
					glVertex3f( ((float)tags[i].x+0.5+0.5*tags[i].size)/10,((float)tags[i].y+0.05)/10,((float)tags[i].z+4*tags[i].size)/40);
					glVertex3f( ((float)tags[i].x+0.5+0.5*tags[i].size)/10,((float)tags[i].y+0.05)/10,((float)tags[i].z)/40);
					glVertex3f( ((float)tags[i].x+0.5-0.5*tags[i].size)/10,((float)tags[i].y+0.05)/10,((float)tags[i].z)/40);
					glVertex3f( ((float)tags[i].x+0.5-0.5*tags[i].size)/10,((float)tags[i].y+0.05)/10,((float)tags[i].z+4*tags[i].size)/40);
				}
				else if(tags[i].rot == 1)
				{
					glVertex3f( ((float)tags[i].x+0.5+0.5*tags[i].size)/10,((float)tags[i].y+0.95)/10,((float)tags[i].z+4*tags[i].size)/40);
					glVertex3f( ((float)tags[i].x+0.5+0.5*tags[i].size)/10,((float)tags[i].y+0.95)/10,((float)tags[i].z)/40);
					glVertex3f( ((float)tags[i].x+0.5-0.5*tags[i].size)/10,((float)tags[i].y+0.95)/10,((float)tags[i].z)/40);
					glVertex3f( ((float)tags[i].x+0.5-0.5*tags[i].size)/10,((float)tags[i].y+0.95)/10,((float)tags[i].z+4*tags[i].size)/40);
				}
			}
		}
		
		//Deco arent covered here
		
		//Poles
		glColor3f(1.0f,1.0f,0.0f);
		for(i=0;i<16;i++)
		{
			if(poles[i].z != 0)
			{
				glVertex3f( ((float)poles[i].x)/10,((float)poles[i].y)/10,((float)poles[i].z)/40);
				glVertex3f( ((float)poles[i].x)/10,((float)poles[i].y)/10,0.0);
				glVertex3f( ((float)poles[i].x)/10,((float)poles[i].y-0.05)/10,0.0);
				glVertex3f( ((float)poles[i].x)/10,((float)poles[i].y-0.05)/10,((float)poles[i].z)/40);
			}
		}
		
		glColor3f(1.0f,0.0f,1.0f);
		glVertex3f(0.0,0.0, -0.025);
		glVertex3f(2.0,0.0, -0.025);
		glVertex3f(2.0,2.0, -0.025);
		glVertex3f(0.0,2.0, -0.025);
	
	
	
		glEnd();
		//glPopMatrix(1);
	
		glPolyFmt(POLY_ALPHA(31) | POLY_CULL_FRONT);
		glColor3f(1.0f,1.0f,1.0f);
	//}
	return;
}

